#include "view_weather.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "sports_mode.h"
#include "font_mngr.h"
#include "app_trace.h"
#include "times/view_time_0.h"
#include "times/view_time_19.h"
#include "view_weather.inc"
#include "weather_ctrl.h"
#include "math.h"
#define LOG(fmt, ...) //app_trace_log(fmt "\r\n", ##__VA_ARGS__)

void the_weather_display(void)
{
    int minimum_temperature = disp_ctrl.com_data.i_temp[19];
    int maximum_temperature = disp_ctrl.com_data.i_temp[20];
    int x = 60 - 26;
    if (disp_ctrl.com_data.i_temp[22] == 0)
    {
        disp_draw_rect_color(x+20, 193 + 7, x+20 + 9, 193 + 8, 0xa514);
        x += 11;
        disp_draw_rect_color(x+20, 193 + 7, x+20 + 9, 193 + 8, 0xa514);
        x += 11;
    }
    else
    {
        if (minimum_temperature < 0)
        {
            disp_draw_rect_color(x, 193 + 7, x + 9, 193 + 8, 0xa514);
            x += 11;
            minimum_temperature = -minimum_temperature;
        }
        disp_draw_user_image(x, 193, img_the_weather_num_TAB[minimum_temperature / 10]);
        x += 11;
        disp_draw_user_image(x, 193, img_the_weather_num_TAB[minimum_temperature % 10]);
        x += 9;
				
				if (maximum_temperature < 0)
        {
            disp_draw_rect_color(x, 193 + 7, x + 9, 193 + 8, 0xa514);
            x += 11;
            maximum_temperature = -maximum_temperature;
        }
				disp_draw_user_image(x, 193, img_the_weather_num_underline);
				x += 9;
        disp_draw_user_image(x, 193, img_the_weather_num_TAB[maximum_temperature / 10]);
        x += 11;
        disp_draw_user_image(x, 193, img_the_weather_num_TAB[maximum_temperature % 10]);
        x += 9;

				    x += 1;
    //如果是华氏度需要转换
			if (disp_ctrl.com_data.i_temp[21] != WEATHER_TEMPERATURE_TYPE_C)
			{
        disp_draw_user_image_ignore_color(x, 193, RGB565_COLOR_BLACK, img_view_fahrenheit);
			}
			else
			{
        disp_draw_user_image_ignore_color(x, 193, RGB565_COLOR_BLACK, img_view_celsius);
			}
    }

    uint8_t weather_icon = 0;
    if (disp_ctrl.com_data.i_temp[22] == 0)
    {
        weather_icon = FILE_IMG_weather_unknown;
    }
    else
    {
        switch (disp_ctrl.com_data.i_temp[18])
        {
        case WEATHER_TYPE_SUNSHINE: //晴天
            weather_icon = FILE_IMG_weather_sun;
            break;
        case WEATHER_TYPE_CLOUDY: //多云
            weather_icon = FILE_IMG_weather_cloudy_day;
            break;
        case WEATHER_TYPE_RAINY: //雨天
            weather_icon = FILE_IMG_weather_rainly;
            break;
        case WEATHER_TYPE_SNOW: //雪
            weather_icon = FILE_IMG_weather_snow;
            break;
        case WEATHER_TYPE_HAZE: //雾霾
            weather_icon = FILE_IMG_weather_foggy_day;
            break;
        case WEATHER_TYPE_UNKNOW:
            weather_icon = FILE_IMG_weather_unknown;
            break;
        }
    }
    if (weather_icon != 0)
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0,10, 43, weather_icon);
    }
}

void view_weather_base_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        weather_record_t weather = {0};
        weather_ctrl_get(0, &weather); //获取天气
        if (weather.date_index == 0)
        {
            disp_ctrl.com_data.i_temp[22] = 0;
        }
        else
        {
            disp_ctrl.com_data.i_temp[22] = 1;
        }
        disp_ctrl.com_data.i_temp[21] = weather_get_temperature_type();
        //如果是华氏度需要转换
        if (disp_ctrl.com_data.i_temp[21] != WEATHER_TEMPERATURE_TYPE_C)
        {
            weather.temperature_min = round(32 + weather.temperature_min * 1.8);
            weather.temperature_max = round(32 + weather.temperature_max * 1.8);
        }
        disp_ctrl.com_data.i_temp[18] = weather.type;
        disp_ctrl.com_data.i_temp[19] = weather.temperature_min;
        disp_ctrl.com_data.i_temp[20] = weather.temperature_max;
    }
		the_weather_display();
		show_time_0_ble(disp_ctrl.com_data.i_temp[5]);
		show_time_0_battery(disp_ctrl.com_data.i_temp[9]);
}



void view_weather_display(int view)
{
    view_weather_base_display();
}
view_event_result_t view_weather_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
